package com.neutch.util;

import com.neutch.constant.Constant;
import com.sun.istack.internal.NotNull;

import java.sql.*;

public class DBUtil {
    //封装连接mysql的CRUD操作 增删改查
    public DBUtil(){}
    public static Connection getConnection(){
        Connection conn = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = getConnection(Constant.URL,Constant.USERNAME,Constant.PASSWORD);

        }catch (Exception e){
            e.printStackTrace();
        }
        return conn;
    }
    public static Connection getConnection(String url,String username,String password){
        Connection coon = null;
        try{
            coon = DriverManager.getConnection(url,username,password);
        }catch (Exception e){
            e.printStackTrace();
        }
        return coon;
    }
    public static Statement getstatement(Connection conn){
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
        }catch (Exception e){
            e.printStackTrace();
        }
        return stmt;
    }
    public static int excuteUpdate(Statement stmt,String sql){
        int rows = 0;
        try {
            rows =  stmt.executeUpdate(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return rows;
    }
    public static void close(@NotNull Statement stmt){
        try {
            if (stmt != null){
                stmt.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void close( @NotNull Connection conn){
        try {
            if (conn != null){
                conn.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void close(@NotNull Statement stmt, Connection conn) {
        close(stmt);
        close(conn);
    }

    public static void close(@NotNull ResultSet rs){
        try {
            if (rs != null){
                rs.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    public static void close(@NotNull Statement stmt, Connection conn,@NotNull ResultSet rs) {
        close(rs);
        close(stmt,conn);
    }
    public static ResultSet getResultSet(Statement stmt,String sql){
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return rs;
    }
    public static ResultSet getResultSet(PreparedStatement prep){
        ResultSet rs = null;
        try {
            rs = prep.executeQuery();
        }catch (Exception e){
            e.printStackTrace();
        }
        return rs;


    }
    public static PreparedStatement getprepareStatement(Connection conn,String sql){
        PreparedStatement prep = null;
        try {
            prep = conn.prepareStatement(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return prep;
    }
    public static void rollback(Connection conn){
        //当出现任何异常时事务不提交回滚到出现异常之前的状态
        try {
            conn.rollback();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}